package admin_menu_api

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/models/res"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/sirupsen/logrus"
)

type MenuDetailsReq struct {
	ID uint `json:"id" form:"id"`
}

func (AdminMenuApi) MenuDetails(c *gin.Context) {
	// 获取url上的参数:id
	//idParam := c.Param("id")
	// 这个才是获取?后面的参数
	//idParam := c.Query("id")
	var req MenuDetailsReq
	if err1 := c.ShouldBindQuery(&req); err1 != nil {
		logrus.Error(err1)
		panic(err1)
	}
	var menuModel models.MenuModel
	err := global.DB.Take(&menuModel, req.ID).Error
	// 数据为空也会有err错误
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	var menu2Image []models.Menu2ImageModel
	err = global.DB.Preload("ImageModel").Order("sort desc").Find(&menu2Image, "menu_id = ? ", req.ID).Error
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	var menu2ImageResponseList []Menu2ImageRsp
	for _, menu2 := range menu2Image {
		menu2ImageResponseList = append(menu2ImageResponseList, Menu2ImageRsp{
			ImageId:   menu2.ImageModel.ID,
			ImageName: menu2.ImageModel.Name,
			ImagePath: menu2.ImageModel.Path,
			Sort:      menu2.Sort,
		})
	}
	var rsp MenuRsp
	copier.Copy(&rsp, &menuModel)
	rsp.ImageList = menu2ImageResponseList
	res.OkWithDate(rsp, c)
}
